package com.frt.sygang.controller;
/*
 * Created by 冯瑞涛 on 2022/9/18 18:52
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.dto.SetmealDto;
import com.frt.sygang.entity.Category;
import com.frt.sygang.entity.Setmeal;
import com.frt.sygang.entity.SetmealDish;
import com.frt.sygang.sevice.CategoryService;
import com.frt.sygang.sevice.SetmealDishService;
import com.frt.sygang.sevice.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/18 18:52
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService dishService;

    /**
     * 分页查询套餐方法
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage> pageQuerySetmeal(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> pages = new Page<>(page, pageSize);
        IPage<SetmealDto> iPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name), Setmeal::getName, name);
        setmealService.page(pages, queryWrapper);
        BeanUtils.copyProperties(pages, iPage, "records");
        List<Setmeal> records = pages.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map(temp -> {
            Long categoryId = temp.getCategoryId();
            Category category = categoryService.getById(categoryId);
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(temp, setmealDto);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        iPage.setRecords(setmealDtos);
        return R.success(iPage);
    }

    /**
     * 添加套餐方法
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#setmealDto.categoryId+'_1'")
    @PostMapping
    public R<String> insSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.insSetmeakAndSetmaekDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 修改套餐状态方法
     * @param status 要修改的状态
     * @param ids //需要修改状态的套餐id
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> setSetmealStatus(@PathVariable Integer status,Long[] ids){
        List<Long> list= Arrays.asList(ids);
        LambdaUpdateWrapper<Setmeal> updateWrapper=new LambdaUpdateWrapper();
        updateWrapper.set(Setmeal::getStatus,status).in(Setmeal::getId,list);
        setmealService.update(updateWrapper);
        return R.success("操作成功");
    }

    /**
     * 根据id获取套餐信息
     * @param id 需要查询的套餐id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealById(@PathVariable Long id){
        SetmealDto setmealById = setmealService.getSetmealById(id);
        return R.success(setmealById);
    }


    /**
     * 修改套餐及关联菜品方法
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#setmealDto.categoryId+'_1'")
    @PutMapping
    public R<String> setSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.setSetmeal(setmealDto);
        return R.success("修改成功");
    }

    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids){
        setmealService.deleteSetmeal(ids);
        return R.success("删除成功");
    }

    @Cacheable(value = "setmealCache",key = "'setmeal_'+#categoryId+'_1'")
    @GetMapping("/list")
    public R<List<SetmealDto>> getSetmealByCategory(Long categoryId){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(queryWrapper);
        List<SetmealDto> setmealDtos = list.stream().map(temp -> {
            LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
            SetmealDto setmealDto = new SetmealDto();
            Long id = temp.getId();
            wrapper.eq(SetmealDish::getDishId, id);
            List<SetmealDish> dishes = dishService.list(wrapper);
            BeanUtils.copyProperties(temp, setmealDto);
            setmealDto.setSetmealDishes(dishes);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(setmealDtos);
    }
}
